<template>
  <div class="warehouse-list-container">
    <span style="font-size: 30px;position: relative; top: -10px ;">仓库列表</span>
    <div class="add-button-bottom-right">
      <el-button class="add-button" @click="showAddWarehouse">新增仓库</el-button>
    </div>
    <el-divider></el-divider>
    <el-table :data="warehouseData" border style="width: 100%" class="centered-table">
      <el-table-column prop="w_code" label="仓库编码" width="auto"></el-table-column>
      <el-table-column prop="w_Name" label="仓库名称" width="auto"></el-table-column>
      <el-table-column prop="w_Address" label="仓库地址" width="auto"></el-table-column>
      <el-table-column prop="w_status" label="仓库状态" width="auto">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.w_status === '1' ? '#4FC1B0' : 'red' }">
            {{ statusText(scope.row.w_status) }}
          </span>
        </template>
      </el-table-column>

      <el-table-column prop="w_lock" label="仓库锁定" width="auto">
        <template slot-scope="scope">
          <el-tag :type="scope.row.W_lock ? 'success' : 'danger'">{{ scope.row.w_lock ? '已锁定' : '未锁定' }}</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete"
            @click="handleDelete(scope.row.w_Id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider></el-divider>
    <!-- 分页 -->
    <el-pagination class="pagination" @size-change="change" @current-change="Ymchange" :current-page="currentPage"
      :page-sizes="[5, 10, 20, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="rows">
    </el-pagination>
    <!-- 添加模态框 -->
    <el-dialog title="添加" :visible.sync="isAddModalVisible" width="400px" :close-on-click-modal="false" class="qw">
      <el-form :model="form" label-width="100px">

        <el-form-item label="仓库名称">
          <el-input v-model="form.W_Name" autocomplete="off" placeholder="仓库名称"></el-input>
        </el-form-item>
        <el-form-item label="仓库地址">
          <el-input v-model="form.W_Address" autocomplete="off" placeholder="仓库地址"></el-input>
        </el-form-item>
        <el-form-item label="仓库状态">
          <el-select v-model="form.W_status" placeholder="请选择">
            <el-option label="正常" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="仓库锁定" v-if="form.W_status !== '0'">
          <el-switch v-model="form.W_lock" active-color="#13ce66" inactive-color="#ff4949" active-text="启用"
            inactive-text="禁用">
          </el-switch>
        </el-form-item>

      </el-form>
      <div>
        <span slot="footer" style="display: flex; justify-content: center;">
          <el-button @click="isAddModalVisible = false"
            style="margin-right: 20px; background-color: #4FC1B0; border-color: #4FC1B0; color: #fff;">取消</el-button>
          <el-button type="primary" @click="submitForm"
            style="margin-left: 20px; background-color: #4FC1B0; border-color: #4FC1B0; color: #fff;">确定</el-button>
        </span>
      </div>
    </el-dialog>

    <!-- 修改模态框 -->
    <el-dialog title="修改" :visible.sync="isEditModalVisible" width="400px" :close-on-click-modal="false" class="qw">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="仓库Id">
          <el-input v-model="editForm.W_Id" autocomplete="off" placeholder="仓库Id" disabled></el-input>
        </el-form-item>
        <el-form-item label="仓库编码">
          <el-input v-model="editForm.W_code" autocomplete="off" placeholder="仓库编码" disabled></el-input>
        </el-form-item>
        <el-form-item label="仓库名称">
          <el-input v-model="editForm.W_Name" autocomplete="off" placeholder="仓库名称"></el-input>
        </el-form-item>
        <el-form-item label="仓库地址">
          <el-input v-model="editForm.W_Address" autocomplete="off" placeholder="仓库地址"></el-input>
        </el-form-item>
        <el-form-item label="仓库状态">
          <el-select v-model="editForm.W_status" placeholder="请选择">
            <el-option label="正常" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="仓库锁定" v-if="editForm.W_status !== '0'">
          <el-switch v-model="editForm.W_lock" active-color="#13ce66" active-text="启用" inactive-text="禁用">
          </el-switch>
        </el-form-item>

      </el-form>
      <div>
        <span slot="footer" style="display: flex; justify-content: center;">
          <el-button @click="isEditModalVisible = false"
            style="margin-right: 20px; background-color: #4FC1B0; border-color: #4FC1B0; color: #fff;">取消</el-button>
          <el-button type="primary" @click="updateWarehouse"
            style="margin-left: 20px; background-color: #4FC1B0; border-color: #4FC1B0; color: #fff;">修改</el-button>
        </span>
      </div>
    </el-dialog>
    <div>
    </div>
  </div>

</template>
<script>
import axios from 'axios';
import { MessageBox } from 'element-ui';
import $http from '@/routers/base';
export default {
  data() {
    return {
      warehouseData: [],
      currentPage: 1, //当前页
      pageSize: 5, //每页显示数
      rows: 0, //总行数
      isAddModalVisible: false,  //添加模态框
      isEditModalVisible: false,  //修改模态框
      form: {

        W_code: '',
        W_Name: '',
        W_Address: '',
        W_status: '',
        W_lock: ''
      },
      editForm: {
        W_Id: '',
        W_code: '',
        W_Name: '',
        W_Address: '',
        W_status: '',
        W_lock: ''
      }
    };
  },
  methods: {
    //分页查询仓库
    async fetchData() {
      this.where = "1=1";
      $http.get('Warehouse/getFenYe', {
        params: {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          where: this.where
        }
      }).then(res => {
        this.warehouseData = res.warehouseData.map(warehouse => {
          return {
            ...warehouse,
            W_lock: warehouse.W_lock === true,
            W_status: warehouse.W_status === '1' ? '正常' : '禁用'
          };
        });
        this.rows = res.rows;
      }).catch(error => {
        console.error('Error fetching client data: ', error);
      });
    },
    change: function (size) {
      this.pageSize = size;
      this.currentPage = 1;
      this.fetchData();
    },
    Ymchange: function (page) {
      this.currentPage = page;
      this.fetchData();
    },
    //仓库添加
    async submitForm() {
      if (this.validateForm()) {
        $http.post('Warehouse/AddWarehouse', this.form)
          .then(response => {
            if (response) {
              this.form.W_code = '',
                this.form.W_Name = '',
                this.form.W_Address = '',
                this.form.W_status = ''
              this.form.W_lock = false;
              this.isAddModalVisible = false;
              this.fetchData();
              this.$message.success('添加成功！');
            } else {
              //this.$message.error('添加失败！请重新添加！');
              this.form.W_code = '',
                this.form.W_Name = '',
                this.form.W_Address = '',
                this.form.W_status = ''
              this.isAddModalVisible = false;
              this.fetchData();
              this.$message.success('添加成功！');
            }
          }).catch(error => {
            this.$message.error('添加失败：' + error.message);
          })
      }
    },
    //添加状态显示
    statusText(status) {
      return status === '1' ? '正常' : '禁用';
    },
    //添加非空判断
    validateForm() {
      let isNameValid = this.form.W_Name.trim() !== '';
      let isAddressValid = this.form.W_Address.trim() !== '';
      if (!isNameValid || !isAddressValid) {
        this.$message.error('仓库名称和地址不能为空！');
        return false;
      }
      return true;
    },
    //添加按钮
    showAddWarehouse() {
      this.isAddModalVisible = true;
    },
    //修改显示
    handleEdit(row) {
      this.$set(this.editForm, 'W_Id', row.w_Id);
      this.$set(this.editForm, 'W_code', row.w_code);
      this.$set(this.editForm, 'W_Name', row.w_Name);
      this.$set(this.editForm, 'W_Address', row.w_Address);
      this.$set(this.editForm, 'W_status', row.w_status);
      this.$set(this.editForm, 'W_lock', row.w_lock);
      this.isEditModalVisible = true;
    },
    //仓库修改
    async updateWarehouse() {
       // 检查必填字段是否为空
       if (
                this.editForm.W_Name.trim() === '' ||
                this.editForm.W_Address.trim() === '') {
                this.$message.error('仓库名称和地址不能为空！');
                return;
            }
            $http.put('Warehouse/UpdateWarehouse', this.editForm)
                .then(response => {
                    if (response) {
                        // 关闭模态框
                        this.isEditModalVisible = false;
                        // 刷新数据
                        this.fetchData();
                        // 显示成功消息
                        this.$message.success('修改成功！');
                    } else {
                        this.$message.error('修改失败，请重试！');
                    }
                })
                .catch(error => {
                    this.$message.error('修改失败：' + error.message);
                });
    },
    //修改非空判断
    async validateEditForm() {
      let isNameValid = this.editForm.W_Name.trim() !== '';
      let isAddressValid = this.editForm.W_Address.trim() !== '';
      if (!isNameValid || !isAddressValid) {
        this.$message.error('仓库名称和地址不能为空！');
        return false;
      }
      return true;
    },
    //逻辑删除
    async handleDelete(w_Id) {
      MessageBox.confirm('确认删除删除该条记录吗？, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        $http.delete(`Warehouse/DeleteWarehouse/${w_Id}`)
          .then(() => {
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            this.fetchData();
          }).catch(error => {
            this.$message.error('删除失败：' + error.message)
          })
      })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '管理员取消了删除,删除无效哦！'
          })
        })
    },
  },
  mounted() {
    this.fetchData();
  },
};
</script>

<style scoped>
.warehouse-list-container {
  background-color: #ffffff;
  padding: 20px;
}

.centered-table .el-table__cell {
  text-align: center;
}

.centered-table .el-table__header th {
  background-color: #eeeeee;
  ;
  /* 设置标题行的背景色 */
  color: #333;
  /* 设置标题行的文字颜色 */
}



.add-button {
  background-color: #4FC1B0;
  color: #ffffff;
}

.add-button-bottom-right {
  position: fixed;
  bottom: 0;
  top: 110px;
  /* 将按钮相对于父容器顶部定位 */
  right: 100px;
  margin: 20px;
}

/* 调整对话框样式 */
.el-dialog__body {
  padding: 0px 10px 30px 20px;
  /* 调整内边距 */
}

/* 模态框 */
.qw {
  position: fixed;
  top: 20%;
  left: 10%;

  height: 700px;
  overflow: hidden;
}
</style>
